package com.vichat.task;

import com.alibaba.dubbo.container.spring.SpringContainer;
import com.vichat.task.entity.CfgTaskParam;
import com.vichat.task.service.ITask;
import com.vichat.task.service.ITaskService;
import com.vichat.user.service.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by linzp on 2019/1/5.
 * 通用sql执行任务，执行频率1天1次
 */
@Component("CommonDayTask")
public class CommonDayTask implements ITask {

    private static transient final Logger logger = LoggerFactory.getLogger(CommonDayTask.class);


    @Override
    public String doTask(long cfgTaskId) throws Exception {

        ApplicationContext ctx = SpringContainer.getContext();
        IUserService userService = (IUserService) ctx.getBean("userService", IUserService.class);
        ITaskService taskService = (ITaskService) ctx.getBean("taskService", ITaskService.class);
        List<CfgTaskParam> params = taskService.getCfgTaskParamById(cfgTaskId);
        if(params != null && !params.isEmpty()){
            for(CfgTaskParam cfgTaskParam : params){
                if(cfgTaskParam.getParamValue().equalsIgnoreCase("sql")){
                    try {
                        userService.excuteTaskSql(cfgTaskParam.getRemark());
                    }catch (Exception e){
                        logger.error("执行sql失败：" + cfgTaskParam.getRemark());
                    }
                }
            }
        }
        return null;
    }
}
